home *** CD-ROM | disk | FTP | other *** search
- /* WHERE.C: will search all DIRs on the given drive for specified
- file. */
-
-
- #include <stdio.h>
- #include <dos.h>
- #include <direct.h>
- #include <conio.h>
- #include <string.h>
- #include <malloc.h>
-
- int count=0;
-
- main(int argc, char *argv[])
- {
- char *curdir,
- sought[80],
- *temp;
- int curdrive, newdrive, p, n=4;
- void searchdir(char *dir, char *ptrn);
-
- /* Find out where we are */
-
- curdir=getcwd(NULL,80);
- _dos_getdrive(&curdrive);
-
- /* Find out what we're looking for */
-
- if(argc>1)
- strcpy(sought,argv[1]);
- else
- {
- printf("\n\nPattern to search for: ");
- gets(sought);
- }
-
- /* Get designator for another drive if specified */
-
- if(sought[1]==':')
- {
- newdrive=(toupper(sought[0]))-64; /* convert */
- _dos_setdrive(newdrive,&n);
- p = (sought[2]=='\\') ? 3:2;
- strcpy(sought, &(sought[p]));
- }
-
- /* Add wildcard prefix/suffix if necessary */
-
- if(sought[0]=='.')
- {
- temp=strcat("*",sought); /* set prefix */
- strcpy(sought,temp);
- }
- if(!strchr(sought,'.'))
- strcpy(sought,"*.*"); /* set suffix */
-
- /* Perform search for pattern starting in root */
-
- searchdir("\\",sought);
- printf("\nNumber of matches: %d",count);
-
- /* Restore Original Drive and Directory */
-
- _dos_setdrive(curdrive,&n);
- chdir(curdir);
- }
-
- /*------------------------------------------------------------------------- */
-
- void searchdir(char *path, char *ptrn)
- #define ANYFILE 0xFF /* recursive routine */
- {
- struct find_t *f;
- char *wholepath;
- unsigned rtn;
-
- chdir(path); /* change to new path */
- wholepath=getcwd(NULL,80); /* get full path name */
- f=malloc(sizeof(*f));
-
- /* Search for filename matches in this directory */
-
- rtn= _dos_findfirst(ptrn,ANYFILE,f);
- while(rtn==0)
- {
- if( f->attrib != _A_SUBDIR )
- printf("%s\\%s\n",wholepath,f->name);
- else printf("%s\\%s <DIR>\n",wholepath, f->name);
- ++count;
-
- rtn = _dos_findnext(f); /* find next match */
- } /* end while loop */
-
- /* Now search any subdirectories under this directory */
-
- rtn= _dos_findfirst("*.*", _A_SUBDIR,f);
- while(rtn==0)
- {
- if( (f->attrib == _A_SUBDIR) && (f->name[0] != '.'))
- {
- searchdir(f->name,ptrn); /* recursive call */
- chdir(wholepath);
- }
- rtn = _dos_findnext(f); /* search next dir */
- }
-
- free(wholepath);
- free(f);
- }
-